﻿using NHibernate;

namespace Laugh.Data.DatabaseCfg
{
    /// <summary>
    /// 继承本类配置数据库连接信息，实现访问数据库，包括ORACEL，SQL SERVER，MYSQL
    /// </summary>
    public abstract class Database : IDatabase
    {
        private IDatabaseBuilder _databaseBuilder;
        private DatabaseModel Mode { get; set; }

        public void Register(ISessionFactoryHolder holder)
        {
            _databaseBuilder = new DatabaseBuilder();

            DatabaseConfiguration(_databaseBuilder);

            Mode = _databaseBuilder.Build();

            holder.Register(Mode.Name, Mode.SessionFactory, null, null);
        }

        /// <summary>
        /// 实现本方法用于注册数据库，使用时，配置文件写为以下内容
        /// dbBuilder.Named("default.database").ConnectionString("Data Source=c:\\sm.db;Version=3;New=True;").UsingDatabase(DatabaseType.SQLite);
        /// </summary>
        /// <param name="dbBuilder"></param>
        public abstract void DatabaseConfiguration(IDatabaseBuilder dbBuilder);

        #region IDatabase接口成员

        public void BuildSchema()
        {
            _databaseBuilder.BuildSchema();
        }

        public ISessionFactory SessionFactory { get { return Mode.SessionFactory; } }

        #endregion
    }
}
